<template>
	<view class="logistics-detail">
		<view class="logistics-info" v-if="orderInfo">
			<view class="status-section">
				<text class="status">{{ getStatusText(orderInfo.status) }}</text>
			</view>
			
			<view class="address-section" v-if="addressInfo">
				<view class="user-info">
					<text class="name">{{ addressInfo.name }}</text>
					<text class="phone">{{ addressInfo.phone }}</text>
				</view>
				<view class="address">
					{{ addressInfo.province }}{{ addressInfo.city }}{{ addressInfo.district }}({{ addressInfo.addressDetail }})
				</view>
			</view>
			
			<view class="goods-section">
				<view class="goods-info">
					<image class="goods-img" :src="orderInfo.mallGoodsVo.mainImages[0].imageUrl" mode="aspectFill"></image>
					<view class="goods-detail">
						<view class="goods-name">{{ orderInfo.goodsName }}</view>
						<view class="goods-count">
							<text class="count">数量：{{ orderInfo.goodsCount }}</text>
						</view>
					</view>
				</view>
			</view>
			
			<view class="logistics-section" v-if="orderInfo.trackingNumber">
				<view class="logistics-header">
					<image class="logo" :src="logisticsInfo && logisticsInfo.logisticsLogo" mode="aspectFit" v-if="logisticsInfo && logisticsInfo.logisticsLogo"></image>
					<view class="info">
						<view class="company">
							<text class="name">{{ logisticsInfo && logisticsInfo.logisticsName }}</text>
							<text class="phone">{{ logisticsInfo && logisticsInfo.logisticsPhone }}</text>
						</view>
						<view class="tracking">
							<text class="label">物流单号：</text>
							<text class="value">{{ formatTrackingNumber(orderInfo.trackingNumber) }}</text>
						</view>
					</view>
				</view>
				
				<view class="logistics-timeline" v-if="logisticsInfo && logisticsInfo.details">
					<view class="latest-status" v-if="logisticsInfo.latestStatus">
						<text class="time">{{ formatDate(logisticsInfo.latestTime) }}</text>
						<text class="status">{{ logisticsInfo.latestStatus }}</text>
					</view>
					<view class="timeline-list">
						<view class="timeline-item" v-for="(item, index) in logisticsInfo.details" :key="index">
							<view class="time">{{ formatDate(item.trackingTime) }}</view>
							<view class="content">{{ item.trackingStatus }}</view>
						</view>
					</view>
				</view>
			</view>
			
			<view class="order-info">
				<view class="order-no">
					<text class="label">订单编号：</text>
					<text class="value">{{ orderInfo.orderNo }}</text>
				</view>
				<view class="create-time">
					<text class="label">创建时间：</text>
					<text class="value">{{ formatDate(orderInfo.createTime) }}</text>
				</view>
				<view class="pay-time" v-if="orderInfo.payTime">
					<text class="label">支付时间：</text>
					<text class="value">{{ formatDate(orderInfo.payTime) }}</text>
				</view>
				<view class="finish-time" v-if="orderInfo.finishTime">
					<text class="label">完成时间：</text>
					<text class="value">{{ formatDate(orderInfo.finishTime) }}</text>
				</view>
				<view class="payment-type">
					<text class="label">支付方式：</text>
					<text class="value">{{ getPaymentTypeText(orderInfo.paymentType) }}</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { formatDate } from '@/utils/date.js'
	
	export default {
		data() {
			return {
				orderId: 0,
				orderInfo: null,
				addressInfo: null,
				logisticsInfo: null
			}
		},
		
		onLoad(options) {
			this.orderId = options.orderId
			this.getOrderDetail()
		},
		
		methods: {
			async getOrderDetail() {
				try {
					const res = await this.$http.get('/merchant/order/detail?orderId=' + this.orderId)
					
					if (res) {
						this.orderInfo = res
						if (this.orderInfo.addressId) {
							this.getAddressInfo(this.orderInfo.addressId)
						}
						if (this.orderInfo.trackingNumber) {
							this.getLogisticsInfo()
						}
					}
				} catch (e) {
					uni.showToast({
						title: '获取订单详情失败',
						icon: 'none'
					})
				}
			},
			
			async getAddressInfo(addressId) {
				try {
					const res = await this.$http.get(`/address/${addressId}`)
					if (res) {
						this.addressInfo = res
					}
				} catch (e) {
					uni.showToast({
						title: '获取地址信息失败',
						icon: 'none'
					})
				}
			},
			
			
			async getLogisticsInfo() {
				try {
					let trackingNumber = this.orderInfo.trackingNumber
					console.log(trackingNumber)
					const res = await this.$http.get(`/logistics/tracking/${trackingNumber}`)
					if (res) {
						this.logisticsInfo = res
					}
				} catch (e) {
					uni.showToast({
						title: '获取物流信息失败',
						icon: 'none'
					})
				}
			},
			
			formatDate(date) {
				return formatDate(date)
			},
			
			getStatusText(status) {
				const statusMap = {
					0: '待支付',
					1: '待发货',
					2: '已完成',
					3: '已取消',
					4: '已退款',
					5: '超时未支付',
					6: '待收货'
				}
				return statusMap[status] || '未知状态'
			},
			
			getPaymentTypeText(type) {
				const paymentMap = {
					1: '微信支付',
					2: '积分支付',
					3: '积分-金额混合支付',
					4: '礼品卡支付',
					5: '礼品券支付'
				}
				return paymentMap[type] || '未知方式'
			},
			
			formatTrackingNumber(trackingNumber) {
				if (!trackingNumber) return '';
				const parts = trackingNumber.split(':');
				return parts[0];
			}
		}
	}
</script>

<style lang="scss" scoped>
	@import 'logistics_detail.scss';
</style>
